package com.ship.dispatch.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ship.dispatch.bean.SpFile;
import com.ship.dispatch.bean.SpOverhaul;
import com.ship.dispatch.bo.SpOverhaulBO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

public interface SpOverhaulServiceMapper extends BaseMapper<SpOverhaul> {
    /**
     * 查询列表展示信息
     * @return
     */
    @Select("SELECT id,name_of_vessel,imo,inspection_time,inspection_content,remarks,create_date,create_by,del_apply FROM sp_overhaul WHERE del_apply = 0 ORDER BY create_date DESC")
    List<SpOverhaul> getInfo();

    /**
     * 获取所有船只
     * @return
     */
    @Select("SELECT ship_name FROM con_shipping")
    List<String> getShipList();

    /**
     * 根据船舶名字查询imo
     * @param nameOfVessel
     * @return
     */
    @Select("SELECT imo FROM sp_information WHERE name_of_vessel = #{nameOfVessel}")
    String getImoByName(String nameOfVessel);

    /**
     * 根据船舶名字查询imo
     * @param imo
     * @return
     */
    @Select("SELECT name_of_vessel FROM sp_information WHERE imo = #{imo}")
    String getNameByImo(String imo);

    /**
     * 根据id,动态查询信息
     * @param id
     * @return
     */
    @Select({
            "<script>",
            "SELECT id,name_of_vessel, imo, inspection_time, inspection_content, remarks, create_date, create_by ",
            "FROM sp_overhaul ",
            "WHERE del_apply = 0 AND id IN ",
            "<foreach item='item' index='index' collection='arg0' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"
    })
    List<SpOverhaul> getInFOById(List<String> id);

    /**
     *  根据id查询信息
     */
    @Select("SELECT id, name_of_vessel, imo, inspection_time, inspection_content FROM sp_overhaul WHERE del_apply = 0 AND id = #{id} ")
    SpOverhaul getById(String id);

//    /**
//     * 上传文件
//     * @param spFile
//     */
//    @Insert("INSERT INTO con_files (id, con_id, file_type, file_name, url) " +
//            "VALUES (#{id}, #{conId}, #{fileType}, #{fileName}, #{url})")
//    void saveFile(SpFile spFile);

    /**
     * 分页查询(废弃)
     * @param page
     * @param spOverhaulBO
     * @return
     */
    Page<SpOverhaul> getPage(Page page,@Param(value = "dto") SpOverhaulBO spOverhaulBO);

    /**
     * 根据id获取文件下载路径
     * @param id
     * @return
     */
    @Select("SELECT path FROM sp_fob_file_record WHERE overhaul_id = #{id}")
    List<String> getFileById(Long id);

    /**
     * 获取船名和其imo
     * @return
     */
    @Select("SELECT name_of_vessel AS nameOfVessel, imo FROM sp_information WHERE name_of_vessel NOT LIKE '%虚拟船%'")
    List<Map<String, String>> getShipAndImo();

    /**
     * 根据id返回文件下载路径及其文件id
     * @return
     */
    @Select("SELECT path AS url, id AS fileId FROM sp_fob_file_record WHERE overhaul_id = #{id}")
    List<Map<String, String>> getFileIdById(Long id);

    /**
     * 获取当前的船名和imo
     * @return
     */
    @Select("SELECT name_of_vessel AS nameOfVessel, imo FROM sp_overhaul WHERE del_apply = 0")
    List<Map<String, String>> getNowShipAndImo();

    /**
     * 绑定修改时添加的文件
     * @param id
     * @param quaId
     */
    @Update("UPDATE sp_fob_file_record SET overhaul_id = #{quaId} WHERE id = #{id}")
    void upDateFileId(@Param("id") String id,@Param("quaId") String quaId);
}
